<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class InverterDataTool extends Model
{
    use SoftDeletes;
    // 存储 第18之后的电站数据
    protected $table = 'inverter_datas_four_begin_2019_12_13';

    protected  $fillable = ['device_id','site_id','user_id','DCU1','DCI1','DCU2','DCI2','DCU3','DCI3','DCU4','DCI4','ACUA','ACIA','ACUB','ACIB','ACUC','ACIC','IVTemp','TotGenCap','IVStatus','IVError','IVMode','FR','IVRun','YearGenCap','MonthGenCap','LastMonthGenCap','DayGenCap','LastDayGenCap','PowerLimitPercent','ActivePower','ReactivePower','ApparentPower','PowerFactor','BoardTemp1','BoardTemp2','IVType','EventTime','EventType'];

    public static function getLastInverterDataByDeviceId($user_id,$device_id)
    {
        $inverterData=InverterDataTwo::where('user_id',$user_id)->where('device_id', $device_id)->orderBy('id', 'desc')->first();
        $device=DeviceTwo::findByDeviceId($user_id,$device_id);
        if($inverterData)
        {
            $inverterData->TotGenCap=$inverterData->TotGenCap+$device->offset;
        }
        return $inverterData;
    }

    public static function getLastInverterData($user_id)
    {

        $today_gen_cap = 0;

        $month_gen_cap = 0;

        $total_gen_cap = 0;

        $total_offset = 0;

        $devices = DeviceTwo::getDevicesByUserId($user_id);

        $time = time();

        $count = 0;

        $begin_time = date('Y-m-d', $time).' 00:00:00';

        $end_time = date('Y-m-d H:i:s', $time);

        foreach ($devices as $device) {
            $obj = InverterDataTwo::where('user_id',$user_id)
                ->where('device_id',$device->device_id)
                ->whereBetween('EventTime',[$begin_time,$end_time])
                ->orderBy('id','desc')->first();

            if($obj) {
                $created_at = strtotime($obj->EventTime);
                $now = time();
                if ($now - $created_at < 60 * 60) {
                    //最近一次非警报状态则正常运行
                    $count++;
                }
//                dump($obj);
                $today_gen_cap += $obj->DayGenCap;
                $month_gen_cap += $obj->MonthGenCap;
                $total_gen_cap += $obj->TotGenCap;
                $total_offset += $device->offset;
            }
        }

        $result['today_gen_cap'] = $today_gen_cap;
        $result['month_gen_cap'] = $month_gen_cap;
        $result['total_gen_cap'] = $total_gen_cap;
        $result['total_offset'] = $total_offset;
        $result['count'] = $count;
        return $result;
    }

    public static function getSomeInverterDataByDeviceId($user_id,$device_id, $begin_time, $end_time)
    {
        $star_time=date('Y-m-d H:i:s',strtotime($begin_time));
        $end_time=date('Y-m-d H:i:s',strtotime($end_time));
        $datas= InverterDataTwo::where('device_id',$device_id)
            ->where('user_id',$user_id)
            ->whereBetween('created_at',[$star_time,$end_time])
            ->get();
        return $datas;
    }

    public static function getSomeGenCapByDeviceId($user_id,$device_id, $begin_time, $end_time)
    {
        $first = InverterDataTwo::where('device_id', $device_id)
            ->where('user_id',$user_id)
            ->where('created_at', '>', $begin_time)->first();

        $last = InverterDataTwo::where('device_id', $device_id)
            ->where('user_id',$user_id)
            ->where('created_at', '<', $end_time)->first();

        if (!$first || $last) {
            return 0;
        }
        return $last->TotGenCap - $first->TotGenCap;

    }

    public static function getOneDayGenCapByDeviceId($user_id,$device_id, $day)
    {
        $begin_time = $day . ' 0:0:0';
        $end_time = $day . ' 23:59:59';
        $data = InverterDataTwo::where('device_id', $device_id)
            ->where('user_id',$user_id)
            ->where('EventTime','<',$end_time)
            ->where('EventTime','>',$begin_time)
//            ->whereBetween('IVStatus', [0, 3])
            ->orderBy('id', 'desc')->first();
        if (!$data) {
            return 0;
        }

        return $data->DayGenCap;
    }

    public static function getOneMonthGenCapByDeviceId($user_id,$device_id, $month)
    {
        $firstday = date('Y-m-01', strtotime($month));
        $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
        $firstday = $firstday . ' 0:0:0';
        $lastday = $lastday . ' 23:59:59';

        $data = InverterDataTwo::where('device_id', $device_id)
            ->where('user_id',$user_id)
            ->whereBetween('created_at', [$firstday, $lastday])
//            ->whereBetween('IVStatus', [0, 3])
            ->orderBy('MonthGenCap', 'desc')->first();
        if (!$data) {
            return 0;
        }
        return $data->MonthGenCap;
    }
    public static function getDayMonthYearBySiteId($site_id){
        $day = 0;
        $month = 0;
        $year = 0;
        $total = 0;
        $power = 0;
        $devices = DeviceTwo::getDevicesBySiteId($site_id);
        foreach ($devices as $device) {
            $arr = InverterDataTwo::getAllGenCapByDeviceId($device->user_id, $device->device_id);
            $day += $arr['dayGen'];
            $month += $arr['monthGen'];
            $year += $arr['yearGen'];
            $total += $arr['totalGen'];
            $power += $arr['power'];
        }
        $result['dayGen']= $day;
        $result['monthGen'] = $month;
        $result['yearGen'] = $year;
        $result['totalGen'] = $total;
        $result['power'] = $power;
        return $result;
    }
    public static function getAllGenCapByDeviceId($user_id,$device_id){
        $time = time();

        $begin_time = date('Y-m-d', $time) . ' 0:0:0';
        $end_time = date('Y-m-d', $time) . ' 23:59:59';
        $data = InverterDataTwo::where('device_id', $device_id)
            ->where('user_id',$user_id)
            ->whereBetween('EventTime', [$begin_time, $end_time])
//            ->whereBetween('IVStatus', [0, 3])
            ->orderBy('id', 'desc')->first(['DayGenCap','MonthGenCap','TotGenCap','ActivePower']);
        $statistic = StatisticsDeviceYearEnergy::where('user_id',$user_id)
            ->where('device_id',$device_id)
            ->orderBy('id','desc')->first(['value']);
        if(!$statistic){
            $before = 0;
        }else{
            $before = $statistic->value;
        }
        if(!$data){
            $result['dayGen'] = 0;
            $result['monthGen'] = 0;
            $result['yearGen'] = 0;
            $result['totalGen'] = 0;
            $result['power'] = 0;
            return $result;
        }
        $total = $data->TotGenCap;
        $result['dayGen'] = $data->DayGenCap;
        $result['monthGen'] = $data->MonthGenCap;
        $result['yearGen'] =  $total- $before;
        $result['totalGen'] = $total;
        $result['power'] = $data->ActivePower;
        return $result;

    }

    public static function getTodayGenCapByDeviceId($user_id,$device_id)
    {
        $time = time();
        $begin_time = date('Y-m-d', $time) . ' 0:0:0';
        $end_time = date('Y-m-d', $time) . ' 23:59:59';
        //暂时通过判断功率不为0来判断是有效数据
        $data = InverterDataTwo::where('device_id', $device_id)
            ->where('user_id',$user_id)
            ->whereBetween('EventTime', [$begin_time, $end_time])
//            ->whereBetween('IVStatus', [0, 3])
            ->orderBy('id', 'desc')->first();

        if (!$data) {
            return 0;
        }
        return $data->DayGenCap;
    }

    public static function getMonthGenCapByDeviceId($user_id,$device_id)
    {
        $firstday = date('Y-m-01', time());
        $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
        $firstday = $firstday . ' 0:0:0';
        $lastday = $lastday . ' 23:59:59';

        $data = InverterDataTwo::where('device_id', $device_id)
            ->where('user_id',$user_id)
            ->whereBetween('created_at', [$firstday, $lastday])
//            ->whereBetween('IVStatus', [0, 3])
            ->orderBy('id', 'desc')->first();
        if (!$data) {
            return 0;
        }
        return $data->MonthGenCap;
    }

    public static function getYearGenCapByDeviceId($user_id,$device_id)
    {
        $year=date('Y')-1;
        $begin_time = $year.'-12-15 00:00:01';
        $end_time = $year.'-12-31 23:59:59';
        $last = InverterDataTwo::where('device_id',$device_id)
            ->where('user_id',$user_id)->orderBy('id','desc')->first();
        $before = InverterDataTwo::where('device_id',$device_id)
            ->where('user_id',$user_id)->whereBetween('EventTime',[$begin_time,$end_time])
            ->orderBy('id','desc')->first();
        if(!$last)
        {
            return 0;
        }
        if($before)
        {
            return $last->TotGenCap - $before->TotGenCap;

        }else{
            return $last->TotGenCap;
        }

    }

    public static function getTotalGenCapByDeviceId($user_id,$device_id)
    {
        $offset=0;
        $data = InverterDataTwo::where('device_id', $device_id)
            ->where('user_id',$user_id)
//            ->whereBetween('IVStatus', [0, 3])
            ->orderBy('id', 'desc')->first();
        $device= DeviceTwo::findByDeviceId($user_id,$device_id);
        if($device)
        {
            $offset=$device->offset;
        }
        if (!$data) {
            return 0;
        }
        return $data->TotGenCap+$offset;
    }


    public static function getSomeGenCapBySiteId($site_id, $begin_time, $end_time)
    {
        $value = 0;
        $devices = DeviceTwo::getDevicesBySiteId($site_id);
        foreach ($devices as $device) {
            $value = $value + InverterDataTwo::getSomeInverterDataByDeviceId($device->user_id,$device->device_id, $begin_time, $end_time);
        }
        return $value;
    }

    public static function getOneDayGenCapBySiteId($site_id, $day)
    {
        $value = 0;
        $devices = DeviceTwo::getDevicesBySiteId($site_id);
        foreach ($devices as $device) {
            $value = $value + InverterDataTwo::getOneDayGenCapByDeviceId($device->user_id,$device->device_id, $day);
        }
        return $value;
    }

    public static function getOneMonthGenCapBySiteId($site_id, $month)
    {
        $value = 0;
        $devices = DeviceTwo::getDevicesBySiteId($site_id);
        foreach ($devices as $device) {
            $value = $value + InverterDataTwo::getOneMonthGenCapByDeviceId($device->user_id,$device->device_id, $month);
        }
        return $value;
    }

    public static function getTodayGenCapBySiteId($site_id)
    {
        $value = 0;
        $devices = DeviceTwo::getDevicesBySiteId($site_id);
        foreach ($devices as $device) {
            $value = $value + InverterDataTwo::getTodayGenCapByDeviceId($device->user_id,$device->device_id);
        }
        return $value;
    }

    public static function getMonthGenCapBySiteId($site_id)
    {
        $value = 0;
        $devices = DeviceTwo::getDevicesBySiteId($site_id);
        foreach ($devices as $device) {
            $value = $value + InverterDataTwo::getMonthGenCapByDeviceId($device->user_id,$device->device_id);
        }
        return $value;
    }

    public static function getYearGenCapBySiteId($site_id)
    {
        $value = 0;
        $devices = DeviceTwo::getDevicesBySiteId($site_id);
        foreach ($devices as $device) {
            $value = $value + InverterDataTwo::getYearGenCapByDeviceId($device->user_id,$device->device_id);
        }
        return $value;
    }

    public static function getTotalGenCapBySiteId($site_id)
    {
        $value = 0;
        $devices = DeviceTwo::getDevicesBySiteId($site_id);
        foreach ($devices as $device) {
            $value = $value + InverterDataTwo::getTotalGenCapByDeviceId($device->user_id,$device->device_id);
        }
        return $value;
    }

    public static function getOneDayGenCapByUserId($user_id, $day)
    {
        $value = 0;
        $devices = DeviceTwo::getDevicesByUserId($user_id);
        foreach ($devices as $device) {
            $value = $value + InverterDataTwo::getOneDayGenCapByDeviceId($device->user_id,$device->device_id, $day);
        }
        return $value;
    }

    public static function getTodayGenCapByUserId($user_id)
    {
        $value = 0;
        $devices = DeviceTwo::getDevicesByUserId($user_id);
        foreach ($devices as $device) {
            $value = $value + InverterDataTwo::getTodayGenCapByDeviceId($device->user_id,$device->device_id);
        }
        return $value;
    }

    public static function getMonthGenCapByUserId($user_id)
    {
        $value = 0;
        $sites = Site::getSitesByUserId($user_id);
        foreach ($sites as $site) {
            $value = $value + InverterDataTwo::getMonthGenCapBySiteId($site->id);
        }
        return $value;
    }

    public static function getYearGenCapByUserId($user_id)
    {
        $value = 0;

        $sites = Site::getSitesByUserId($user_id);

        foreach ($sites as $site) {
            $year = date('Y') - 1;
            $obj = StatisticsDeviceYearEnergy::where('site_id',$site->id)->where('date',$year)->first();
            if($obj){
            $value = $value + StatisticsDeviceYearEnergy::where('site_id', $site->id)->where('date',$year)->sum('value');
            }
        }

        return $value;
    }

    public static function getTotalGenCapByUserId($user_id)
    {
        $value = 0;
        $sites = Site::getSitesByUserId($user_id);
        foreach ($sites as $site) {
            $value = $value + InverterDataTwo::getTotalGenCapBySiteId($site->id);
        }
        return $value;
    }


    public static function getWarnHistoryByUserId($user_id, $page_size)
    {
        $warns = InverterDataTwo::where('user_id', $user_id)
            ->whereNotBetween('IVStatus', [0, 3])
            ->orderBy('id', 'desc')
            ->paginate($page_size);
        return $warns;
    }


    public static function getNowPowerBySiteId($site_id)
    {
        $power = 0;
        $devices = DeviceTwo::getDevicesBySiteId($site_id);
        foreach ($devices as $device) {
            $livd = InverterDataTwo::getLastInverterDataByDeviceId($device->user_id,$device->device_id);
            if ($livd) {
                $power = $power + $livd->ActivePower;
            }

        }
        return $power;
    }

    public static function  getTodayTemp($user_id,$device_id)
    {
        $time = time();
        $begin_time = date('Y-m-d', $time) . ' 0:0:0';
        $end_time = date('Y-m-d', $time) . ' 23:59:59';
        //暂时通过判断功率不为0来判断是有效数据
        $data = InverterDataTwo::where('device_id', $device_id)
            ->where('user_id',$user_id)
            ->whereBetween('created_at', [$begin_time, $end_time])
//            ->whereBetween('IVStatus', [0, 3])
            ->orderBy('id', 'desc')->first();

        if (!$data) {
            return 0;
        }
        return $data->IVTemp;
    }

}
